
<%@ page import="auctionhausen.Listing" %>
<!doctype html>
<html>
	<head>
		<meta name="layout" content="main">
		<g:javascript src="show_listing.js" />
		<script type="text/javascript">
	        $(document).ready(function(){
							show_listing_init();
							loadRecentBids("${createLink(controller:'listing', action:'getTopBids')}/${listingInstance?.id}");
	        })
        </script>
		<g:set var="entityName" value="${message(code: 'listing.label', default: 'Listing')}" />
		<title><g:message code="default.show.label" args="[entityName]" /></title>
	</head>
	<body>
	    <g:if test="${listingInstance.expired}">
	       <h1 id="listing_expired_header">Listing Expired</h1>
	    </g:if>
	    <g:else>
			<a href="#show-listing" class="skip" tabindex="-1"><g:message code="default.link.skip.label" default="Skip to content&hellip;"/></a>
			<div id="show-listing" class="content scaffold-show" role="main">
				<h1>Listing: ${listingInstance?.name}</h1>
				<g:if test="${flash.message}">
				<div class="message" role="status">${flash.message}</div>
				</g:if>
				<ol class="property-list listing">
				
					<g:if test="${listingInstance?.name}">
					<li class="fieldcontain">
						<span id="name-label" class="property-label"><g:message code="listing.name.label" default="Name" /></span>
						
							<span class="property-value" aria-labelledby="name-label"><g:fieldValue bean="${listingInstance}" field="name"/></span>
						
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.endDate}">
					<li class="fieldcontain">
						<span id="endDate-label" class="property-label"><g:message code="listing.endDate.label" default="End Date" /></span>
						
							<span class="property-value" aria-labelledby="endDate-label"><g:formatDate date="${listingInstance?.endDate}" /></span>
						
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.startingPrice}">
					<li class="fieldcontain">
						<span id="startingPrice-label" class="property-label"><g:message code="listing.startingPrice.label" default="Starting Price" /></span>
						
							<span class="property-value" aria-labelledby="startingPrice-label">
								<g:formatNumber number="${listingInstance.startingPrice}" type="currency" currencyCode="USD" />
							</span>
						
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.description}">
					<li class="fieldcontain">
						<span id="description-label" class="property-label"><g:message code="listing.description.label" default="Description" /></span>
						
							<span class="property-value" aria-labelledby="description-label"><g:fieldValue bean="${listingInstance}" field="description"/></span>
						
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.winner}">
					<li class="fieldcontain">
						<span id="winner-label" class="property-label"><g:message code="listing.winner.label" default="Winner" /></span>
						
							<span class="property-value" aria-labelledby="winner-label"><g:link controller="customer" action="show" id="${listingInstance?.winner?.id}">${listingInstance?.winner?.encodeAsHTML()}</g:link></span>
						
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.bids}">
					<li class="fieldcontain">
						<span id="bids-label" class="property-label"><g:message code="listing.bids.label" default="Top Bid" /></span>
							
							<!-- Show All Bids
							<g:each in="${listingInstance.bids}" var="b">
							<span class="property-value" aria-labelledby="bids-label"><g:link controller="bid" action="show" id="${b.id}">${b?.encodeAsHTML()}</g:link></span>
							</g:each>
							-->
							<!-- Show only most recent Bid -->
							<span class="property-value" aria-labelledby="bids-label">
							
								<g:if test="${session.user?.isAdmin}">
									<g:link controller="bid" action="show" id="${listingInstance.bids.last().id}">
										<g:formatNumber number="${listingInstance.bids.last().amount}" type="currency" currencyCode="USD" />
									</g:link> &nbsp;
									(by ${listingInstance.bids.last().bidder.email.replaceFirst(/@.*/, "")})
								</g:if>
								<g:else>
									<g:formatNumber number="${listingInstance.bids.last().amount}" type="currency" currencyCode="USD" />
									&nbsp; (by ${listingInstance.bids.last().bidder.email.replaceFirst(/@.*/, "")})
								</g:else>
							</span>
	
					</li>
					<li class="fieldcontain">
						<span id="top-10-bids-label" class="property-label">Top 10 Bids</span>
							
							<!-- Show All Bids
							<g:each in="${listingInstance.bids}" var="b">
							<span class="property-value" aria-labelledby="bids-label"><g:link controller="bid" action="show" id="${b.id}">${b?.encodeAsHTML()}</g:link></span>
							</g:each>
							-->
							<!-- Show only most recent Bid -->
							<span class="property-value" aria-labelledby="top-10-bids-label">
							<div id="top-10-bids"></div>
							</span>
	
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.dateCreated}">
					<li class="fieldcontain">
						<span id="dateCreated-label" class="property-label"><g:message code="listing.dateCreated.label" default="Date Created" /></span>
						
							<span class="property-value" aria-labelledby="dateCreated-label"><g:formatDate date="${listingInstance?.dateCreated}" /></span>
						
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.lastUpdated}">
					<li class="fieldcontain">
						<span id="lastUpdated-label" class="property-label"><g:message code="listing.lastUpdated.label" default="Last Updated" /></span>
						
							<span class="property-value" aria-labelledby="lastUpdated-label"><g:formatDate date="${listingInstance?.lastUpdated}" /></span>
						
					</li>
					</g:if>
				
					<g:if test="${listingInstance?.seller}">
					<li class="fieldcontain">
						<span id="seller-label" class="property-label"><g:message code="listing.seller.label" default="Seller" /></span>
						
							<span class="property-value" aria-labelledby="seller-label">
								<g:if test="${session.user?.isAdmin}">
									<g:link controller="customer" action="show" id="${listingInstance?.seller?.id}">${listingInstance.seller.email.replaceFirst(/@.*/, "")}</g:link>
								</g:if>
								<g:else>
									${listingInstance.seller.email.replaceFirst(/@.*/, "")}
								</g:else>
							</span>
						
					</li>
					</g:if>
				</ol>
				
				<g:if test="${session.user?.isAdmin}">
					<g:form>
						<fieldset class="buttons">
							<g:hiddenField name="id" id="listing_id" value="${listingInstance?.id}" />
							<g:link class="edit" action="edit" id="${listingInstance?.id}"><g:message code="default.button.edit.label" default="Edit" /></g:link>
							<g:actionSubmit class="delete" action="delete" value="${message(code: 'default.button.delete.label', default: 'Delete')}" onclick="return confirm('${message(code: 'default.button.delete.confirm.message', default: 'Are you sure?')}');" />
						</fieldset>
					</g:form>
				</g:if>
				
				<h1>Place Bid on this Listing</h1>
				
				<g:if test="${session.user?.id == listingInstance?.seller?.id}">
					<p>Can't place a bid on your own listing!</p>
				</g:if>
				<g:elseif test="${session.user}">
					<g:hasErrors bean="${listingInstance}">
					<ul class="errors" role="alert">
						<g:eachError bean="${listingInstance}" var="error">
						<li <g:if test="${error in org.springframework.validation.FieldError}">data-field-id="${error.field}"</g:if>><g:message error="${error}"/></li>
						</g:eachError>
					</ul>
					</g:hasErrors>
					<g:formRemote name="addBid" url="[controller:'listing', action: 'addBid']" 
					     onSuccess="addBidSuccess(data)">
						<fieldset class="form">
							<g:render template="bidForm" />
						</fieldset>
						<fieldset class="buttons">
							<input type="submit" name="create"  class="floatleft save" value="Place Bid" />
							<div id="addBidMessageContainer" class="floatleft ui-widget-content ui-corner-all"/>
						</fieldset>
					</g:formRemote>
				</g:elseif>
				<g:else>
					<p>You must be logged in to place a bid. <g:link controller="Customer" action="login">Login</g:link> or <g:link controller="Customer" action="register">Register</g:link> for an account to place a bid.</p>
				</g:else>
				
			</div>
		</g:else>
	</body>
</html>
